<?php
//funkcja do usuwania menu przez nstalacje modulu
function parameters_menu_delete($title, $x) {
	global $conf;

	if(perms_check('admin', 'modules')) {
		$db = new dbquery;
		$db2 = new dbquery;
		$db3 = new dbquery;

		$sql=array();
		$languages = get_lang_list();
		foreach($languages as $l) {
			$sql[]="`title_$l`='$title'";
		}

		if(count($sql)>0)
		$sql="WHERE ".implode(" or ", $sql)." && x='".$x."'";

		$db->query("SELECT * FROM $conf[prefix]menu $sql") or $db->err(__FILE__, __LINE__);

		while($u=$db->fetch_object()) {
			$db2->query("SELECT * FROM $conf[prefix]menu WHERE x = '$u->x' ORDER BY y ASC") or $db->err(__FILE__, __LINE__);
			while($d=$db2->fetch_object()) {
				if($u->y<$d->y) {
					$y=$d->y-1;
					$id_=$d->id;

					$db3->query("UPDATE $conf[prefix]menu SET y='$y' WHERE id=$id_") or $db2->err(__FILE__, __LINE__);
				}
			}
			$db2->query("DELETE FROM $conf[prefix]menu WHERE id=$u->id") or $db->err(__FILE__, __LINE__);
		}
		$db->clear_cache('*'.$x.'*', 'menu/');
	}
}
//

//funkcja do dodawania menu przez instalke modlu
function parameters_menu_add($x='', $access='-1', $perms = NULL, $default_content, $default_title, $pl_content='', $pl_title='') {
	global $conf;
	
	if(!$x) exit;
	
	if(perms_check('admin', 'modules')) {

		$db = new dbquery;
		$db->query("SELECT * FROM $conf[prefix]menu WHERE x = '$x' ORDER BY y DESC") or $db->err(__FILE__, __LINE__);
		$d=$db->fetch_object();

		$y=$d->y+1;
		
		$pl_content=str_replace("'", "\'", $pl_content);
		$default_content=str_replace("'", "\'", $default_content);
		
		$sql1=array();
		$sql2=array();
		$languages = get_lang_list();
		foreach($languages as $l) {
			$sql1[]="`content_$l`, `title_$l`";
			
			if($l=='pl' && $pl_content && $pl_title)
				$sql2[]="'$pl_content', '$pl_title'";
			else
				$sql2[]="'$default_content', '$default_title'";
		}

		$db->query("INSERT INTO $conf[prefix]menu (`id`, `x`, `y`, `status`, `access`, `perms`, ".implode(", ", $sql1).") VALUES ('', '$x', $y, 1, '$access', '$perms', ".implode(", ", $sql2).")") or $db->err(__FILE__, __LINE__);
		$db->clear_cache('*'.$x.'*', 'menu/');
	}
}
//

function menu($x, $template) {
	global $lang, $conf;

	$db = new dbquery;
	
	$db->cache('menu_'.$x.'_'.$GLOBALS['gid'].'_'.$_SESSION['lang_short'], 'menu');
	$db->query("SELECT `id`, `access`, `perms`, `title_$_SESSION[lang_short]`, `content_$_SESSION[lang_short]` FROM $conf[prefix]menu WHERE x='$x' && status='1' ORDER by y ASC") or $db->err(__FILE__, __LINE__);

	$gtype = $GLOBALS['group']['type'];

	$menu_blocks='';
	while($d=$db->fetch_object()) {
		$cnt = "content_$_SESSION[lang_short]";
		$tlt = "title_$_SESSION[lang_short]";

		if($d->$cnt == '' or $d->$tlt == '') {
			$cnt = "content_".$conf['language'];
			$tlt = "title_".$conf['language'];
		}

		$menu_content=read_text_menu($d->$cnt);
		$menu_title=read_text_menu($d->$tlt);

		$ok = 0;

		switch($d->access) {
			case '-3':
				$groups = explode(';', $d->perms);
				if(in_array($GLOBALS['gid'], $groups))
				$ok = 1;
				break;
			case '-2':
				if($_SESSION['logged_in'] == 1)
				break;
			case '-1':
				$ok = 1;
				break;
			case '0':
				if($_SESSION['logged_in'] == 1)
				$ok = 1;
				break;
			case '1':
				if($gtype >= 2)
				$ok = 1;
				break;
			default:
				$opts = explode(' || ', $d->perms);
				$ok = 0;

				for($i = 0; $i < count($opts); $i++) {
					$od = 1;
					$o = explode(' && ', $opts[$i]);
					for($k = 0; $k < count($o); $k++) {
						if(!perms_check($d->access, $o[$k]))
						$od = 0;
					}
					if($od == 1)
					$ok = 1;
				}
				break;
		}
		if($ok == 1)
		$menu_blocks.=string_template(read_file('menu_'.$template.'_small.php'), array('menu_block'=>string_template($menu_content, array('theme_path' => $GLOBALS['theme_path'], 'language' => $_SESSION['lang_short'], 'title' => $menu_title)), 'menu_name' => $menu_title));
	}
	//

	//generating menu_left
	$menu=string_template(read_file('menu_'.$template.'.php'), array('menu_blocks'=>$menu_blocks, 'theme_path' => $GLOBALS['theme_path']));
	//

	return $menu;
}

?>
